package com.youku.plugins.programListV4.view
{
   import flash.display.Sprite;
   import flash.display.MovieClip;
   import com.youku.baseUI.BasePlayerButton;
   import com.youku.plugins.programListV4.view.items.SliderBar;
   import com.youku.interfaces.IProgramData;
   import com.youku.plugins.programListV4.view.items.ProgramListParts;
   import com.youku.utils.AssetUtil;
   import flash.text.TextField;
   import flash.text.TextFormat;
   import com.youku.skin.YaheiFontManager;
   import flash.events.MouseEvent;
   import com.youku.plugins.programListV4.view.event.ProgramListEvent;
   
   public class ProgramList extends Sprite
   {
      
      public function ProgramList() {
         super();
      }
      
      private var _dataArray:Array;
      
      private var _partArray:Array;
      
      private var _stageWidth:Number;
      
      private var _stageHeight:Number;
      
      private var _highlightNum:int;
      
      private var _background:MovieClip;
      
      private var _videoID:String;
      
      private var _layer:Sprite;
      
      private var _closeButton:BasePlayerButton;
      
      private var _partLayer:Sprite;
      
      private var _partLayerMask:Sprite;
      
      private var _thisScale:Number;
      
      private var _slider:SliderBar;
      
      private var _partH:Number;
      
      public function init(param1:Array, param2:String) : void {
         var _loc5_:IProgramData = null;
         var _loc6_:ProgramListParts = null;
         if(param1 == null || param1.length == 0)
         {
            return;
         }
         this._videoID = param2;
         this._dataArray = param1;
         this._thisScale = 1;
         this._layer = new Sprite();
         this.addChild(this._layer);
         this._background = AssetUtil.getAsset("V4_panel_bg") as MovieClip;
         this._background.width = 330;
         this._background.height = 264;
         this._layer.addChild(this._background);
         var _loc3_:TextField = new TextField();
         _loc3_.defaultTextFormat = new TextFormat(YaheiFontManager.MICROSOFT_YAHEI,14,16777215);
         _loc3_.selectable = false;
         _loc3_.x = 8;
         _loc3_.y = 5;
         _loc3_.width = 60;
         _loc3_.height = 24;
         _loc3_.text = "选集:";
         this._layer.addChild(_loc3_);
         this._closeButton = AssetUtil.getStyleButton("P_CloseButton");
         AssetUtil.restoreButtonSize(this._closeButton);
         this._closeButton.x = 306;
         this._closeButton.y = 10;
         this._closeButton.addEventListener(MouseEvent.CLICK,this.onCloseBtnClick);
         this._layer.addChild(this._closeButton);
         this._slider = new SliderBar();
         this._slider.x = 306;
         this._slider.y = 40;
         this._slider.addEventListener(ProgramListEvent.CHANGE,this.onSliderChange);
         if(this._dataArray.length > 5)
         {
            this._layer.addChild(this._slider);
         }
         this._partLayer = new Sprite();
         this._partLayer.x = 20;
         this._partLayer.y = 40;
         this._layer.addChild(this._partLayer);
         this._partH = 0;
         this._partArray = new Array();
         var _loc4_:* = 0;
         while(_loc4_ < this._dataArray.length)
         {
            _loc5_ = this._dataArray[_loc4_] as IProgramData;
            _loc6_ = new ProgramListParts(this._dataArray[_loc4_]);
            _loc6_.x = 0;
            _loc6_.y = (_loc6_.height + 1) * _loc4_;
            this._partLayer.addChild(_loc6_);
            this._partArray.push(_loc6_);
            if(this._videoID == _loc5_.videoID)
            {
               this._highlightNum = _loc4_;
               _loc6_.active();
            }
            this._partH = _loc6_.height;
            _loc4_++;
         }
         this._partLayerMask = new Sprite();
         this._partLayerMask.graphics.beginFill(39372);
         this._partLayerMask.graphics.drawRect(0,0,this._partLayer.width,(this._partH + 1) * 5);
         this._partLayerMask.graphics.endFill();
         this._partLayerMask.x = this._partLayer.x;
         this._partLayerMask.y = this._partLayer.y;
         this._layer.addChild(this._partLayerMask);
         this._partLayer.mask = this._partLayerMask;
      }
      
      private function onCloseBtnClick(param1:MouseEvent) : void {
         this.dispatchEvent(new ProgramListEvent(ProgramListEvent.CLOSE_BTN_CLICK));
      }
      
      protected function onSliderChange(param1:ProgramListEvent) : void {
         var _loc2_:Number = this._slider.value;
         this._partLayer.y = (this._partLayerMask.height - this._partLayer.height) * _loc2_ + this._partLayerMask.y;
      }
      
      public function wheelDown() : void {
         if(this._partLayer.height <= this._partLayerMask.height)
         {
            return;
         }
         this._slider.value = this._slider.value + 0.1;
         if(this._slider.value > 1)
         {
            this._slider.value = 1;
         }
      }
      
      public function wheelUp() : void {
         if(this._partLayer.height <= this._partLayerMask.height)
         {
            return;
         }
         this._slider.value = this._slider.value - 0.1;
         if(this._slider.value < 0)
         {
            this._slider.value = 0;
         }
      }
      
      public function show() : void {
         if(this._dataArray == null || this._dataArray.length == 0 || this._stageWidth < 340 || this._stageHeight < 300)
         {
            this._layer.visible = false;
            return;
         }
         this.changeSize(this._stageWidth,this._stageHeight);
         this._layer.visible = true;
         var _loc1_:Number = 0;
         if(this._dataArray.length > 1)
         {
            _loc1_ = this._highlightNum / (this._dataArray.length - 1);
            _loc1_ = Math.max(_loc1_,0);
            _loc1_ = Math.min(_loc1_,1);
         }
         this._slider.value = _loc1_;
      }
      
      public function changeSize(param1:Number, param2:Number) : void {
         this._stageWidth = param1;
         this._stageHeight = param2;
         if(!this._layer)
         {
            return;
         }
         this._thisScale = this._stageHeight / 500 > 1?this._stageHeight / 500:1;
         this._layer.scaleX = this._layer.scaleY = this._thisScale;
         this._layer.x = (this._stageWidth - this._background.width * this._thisScale) / 2;
         this._layer.y = (this._stageHeight - this._background.height * this._thisScale) / 2;
      }
      
      public function reset() : void {
         if(!this._layer)
         {
            return;
         }
         if(this.contains(this._layer))
         {
            this.removeChild(this._layer);
         }
      }
   }
}
